package com.zgw.his.emr.workbench;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.part.ViewPart;

import com.zgw.his.emr.model.DetailRecord;
import com.zgw.his.emr.model.PageRecord;
import com.zgw.his.emr.model.RootRecord;
import com.zgw.his.emr.model.TemplateRecord;
import com.zgw.his.emr.model.TextRecord;
import com.zgw.his.emr.resource.RecordResourceManager;
import com.zgw.his.emr.resource.TemplateSite;

public class NavigationView extends ViewPart {
	public static final String ID = "com.zgw.his.emr.workbench.NavigationView";
	Tree tree;
	public static DefaultContainer container;
	private int i = 1;

	@Override
	public void createPartControl(Composite parent) {
		parent.setLayout(new FillLayout());
		tree = new Tree(parent, SWT.NONE);
		buildTree();
		tree.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				doClickTree();
			}
		});
		initialize();
	}

	private void initialize() {
		container = new DefaultContainer();
	}

	private void buildTree() {
		tree.removeAll();
		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText("刷新");
		{ // 加载默认模板库
			TreeItem tempItem = new TreeItem(tree, SWT.NONE);
			tempItem.setText("模板库");
			TemplateSite.loadDefault();
			String[] ids = TemplateSite.getTemplateIds();
			if (ids != null) {
				for (String id : ids) {
					TreeItem i = new TreeItem(tempItem, SWT.NONE);
					i.setText(id);
					i.setData(TemplateSite.findPath(id));
				}
			}
		}
	}

	protected void doClickTree() {
		if (tree.getSelection()[0].getData() == null
				&& tree.getSelection()[0].getText().equals("刷新")) {
			buildTree();
		} else {
			tree.getSelection()[0].getText();
			File f = new File((String) tree.getSelection()[0].getData());
			try {
				String content = RecordResourceManager.findRecordContent(f
						.getName());
				if (content != null) {
					container.initialization(content);
				} else {
					container.initialization(createNewRecord(f));
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			container.showPage(container.getPages().get(0));
		}
	}

	private RootRecord createNewRecord(File f) {
		RootRecord record = new RootRecord();
		record.setIdentifier(f.getName());
		record.setVersion("1.0.0");
		PageRecord page = new PageRecord();
		TemplateRecord template = new TemplateRecord();
		template.setIdentifier(f.getName());
		template.setTemplateUrl(TemplateSite.findUrl(f.getName()));
		page.setTemplate(template);
		page.setPageNumber(i);
		record.addChild(page);
		return record;
	}

	public RootRecord createTestRecord() {
		if (tree.getSelection().length <= 0) {
			return null;
		}
		String file = (String) tree.getSelection()[0].getData();
		RootRecord record = new RootRecord();
		record.setIdentifier("recordTest001");
		PageRecord page = new PageRecord();
		TemplateRecord template = new TemplateRecord();
		{
			DetailRecord detail = new TextRecord("1.1");
			detail.setText("张三");
			template.addChild(detail);
		}
		{
			DetailRecord detail = new TextRecord("1.2");
			detail.setText("妇一科");
			template.addChild(detail);
		}
		{
			DetailRecord detail = new TextRecord("1.3");
			detail.setText("001");
			template.addChild(detail);
		}
		{
			DetailRecord detail = new TextRecord("1.4");
			detail.setText("34092");
			template.addChild(detail);
		}
		{
			DetailRecord detail = new TextRecord("1.5");
			detail.setText("患者五日入院，右脚不适，需深化治疗");
			template.addChild(detail);
		}
		try {
			template.setTemplateUrl(new URL("file", "", file));
			template.setIdentifier(file);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		page.setTemplate(template);
		page.setPageNumber(i++);
		record.addChild(page);

		return record;
	}

	@Override
	public void setFocus() {

	}

}
